<?php

namespace App\Services\MockUser;

use App\Models\GoldRound;
use App\Models\GoldProduct;
use App\Jobs\RobotReclimJob;

class StormReclaimer extends MockUser
{
	protected GoldRound $round;

	public function __construct(GoldRound $round)
	{
		$this->round = $round;
	}

	protected function name() : string
	{
		return 'reclaimer';
	}

	protected function phone() : string
	{
		return '18800000001';
	}

	protected function create(GoldProduct $product)
	{
		$queue = app()->environment('local') ? 'default' : 'default';
		dispatch(new RobotReclimJob($this->robot(), $product))->onQueue($queue);
	}

	protected function recall()
	{
		$alives = $this->round->allAliveGoldProductsCache();

		if (count($alives) <= 0) return;

		$rid = $this->round->id;

		foreach ($alives as $pid) {

			GoldProduct::sDelFromAliveCache($pid, $rid);

            if (! $p = GoldProduct::find($pid)) continue;

            if (sc('is_reclaim_on')) {
                $this->create($p);
            }
		}
	}

	public function reclaim()
	{
        $this->recall();
	}
}
